package cn.autoOffice.controller;

import cn.autoOffice.bean.pojo.Discuss;
import cn.autoOffice.bean.pojo.Employee;
import cn.autoOffice.common.utils.Result;
import cn.autoOffice.service.DiscussService;
import cn.autoOffice.service.RedisService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 金健伟
 * @DATE 2023/3/28 11:14
 * 功能说明：
 */

@RestController
@RequestMapping("/discuss")
public class DiscussController {

    @Autowired
    @Resource
    private DiscussService discussService;

    @Autowired
    private RedisService redisService;

    @GetMapping("/getDiscuss")
    // @RequiresPermissions("discuss:select")
    public Result getDiscuss(@RequestParam(value = "empId",required = false) Integer empId,
                             @RequestParam("currentPage") Integer currentPage,
                             @RequestParam("pageSize") Integer pageSize){

        PageInfo pageInfo;
        List<Discuss> discussTitle;
        Integer countDis = discussService.countDiscuss(null);
        String disCount = redisService.getValue("allDis" + currentPage);
        if (disCount != null){
            discussTitle = JSONObject.parseArray(disCount,Discuss.class);
            pageInfo = new PageInfo(discussTitle);
            pageInfo.setTotal(countDis);
        } else {
            PageHelper.startPage(currentPage,pageSize);
            discussTitle = discussService.getDiscussTitle(empId);
            pageInfo = new PageInfo<>(discussTitle);
            redisService.putValue("allDis" + currentPage, discussTitle, 60);
            redisService.putValue("allDisCount",pageInfo.getTotal(),60);
        }

        return Result.success(pageInfo);
    }

    @PostMapping("/addDiscussByTitle")
    // @RequiresPermissions("discuss:add")
    public Result addDiscussByTitle(@RequestBody Discuss discuss){
        Integer integer = discussService.addDiscussByTitle(discuss);
        if (integer > 0){
            return Result.success();
        } else {
            return Result.failure();
        }
    }

    @PostMapping("/deleteDiscussByTitleId")
    // @RequiresPermissions("discuss:delete")
    public Result deleteDiscussByTitleId(@RequestBody Discuss discuss){
        Integer integer = discussService.deleteDiscussByTitleId(discuss);
        if (integer > 0){
            return Result.success();
        } else {
            return Result.failure();
        }
    }

    @GetMapping("/getSelectOneDis")
    // @RequiresPermissions("discuss:select")
    public Result getSelectOneDis(@RequestParam("titleId") Integer titleId,
                                  @RequestParam("currentPage") Integer currentPage,
                                  @RequestParam("pageSize") Integer pageSize){
        PageInfo pageInfo;
        List<Discuss> discussTitle;
        Integer oneCountDis = discussService.countDiscuss(titleId);
        String oneDisCount = redisService.getValue("DisOne" + currentPage);
        if (oneDisCount != null){
            discussTitle = JSONObject.parseArray(oneDisCount,Discuss.class);
            pageInfo = new PageInfo(discussTitle);
            pageInfo.setTotal(oneCountDis);
        } else {
            PageHelper.startPage(currentPage,pageSize);
            discussTitle = discussService.getDiscussByOne(titleId);
            pageInfo = new PageInfo<>(discussTitle);
            redisService.putValue("allDis" + currentPage, discussTitle, 60);
            redisService.putValue("allDisCount",pageInfo.getTotal(),60);
        }

        return Result.success(pageInfo);
    }
}
